/*
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dailydev.wave.robot.spring;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dailydev.wave.robot.IRobot;
import org.dailydev.wave.robot.RobotException;
import org.dailydev.wave.robot.serializer.ISerializer;
import org.dailydev.wave.robot.serializer.ProfileSerializer;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.google.wave.api.ParticipantProfile;

/**
 * @author Roman Bosak, DailyDev.org
 */
public class ProfileController implements Controller {

    /**
     * {@link ProfileController}'s logger
     */
    private static Logger log = Logger.getLogger(RobotController.class.getName());

    /**
     * Logger for response payload
     */
    private static Logger payloadLog = Logger.getLogger("robot.payload.profile");

    /**
     * Reference to served robot
     */
    protected IRobot robot;

    /**
     * Serialiser and deserialiser of message payload.
     */
    protected ISerializer<ParticipantProfile> serializer;

    /**
     * 
     */
    public ProfileController() {
        serializer = new ProfileSerializer();
    }

    /** {@inheritDoc} */
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ParticipantProfile profile = new ParticipantProfile(robot.getRobotName(), robot.getRobotAvatar(), request
                .getRequestURL().toString());

        try {
            String responseBody = serializer.serialize(profile);

            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().write(responseBody);

            if (payloadLog.isLoggable(Level.FINEST)) {
                payloadLog.finest("RESPONSE: 200 OK >>>" + responseBody + "<<<");
            }
        } catch (RobotException e) {
            log.log(Level.SEVERE, "500 - Internal Server Error", e);
            if (payloadLog.isLoggable(Level.FINEST)) {
                payloadLog.finest("RESPONSE: 500 Internal Server Error >>> " + e.getMessage());
            }
            response.setStatus(500);
        }

        return null;
    }

    /**
     * @return the serializer
     */
    public ISerializer<ParticipantProfile> getSerializer() {
        return serializer;
    }

    /**
     * @param serializer
     *            the serializer to set
     */
    public void setSerializer(ISerializer<ParticipantProfile> serializer) {
        this.serializer = serializer;
    }

    /**
     * @return the robot
     */
    public IRobot getRobot() {
        return robot;
    }

    /**
     * @param robot
     *            the robot to set
     */
    public void setRobot(IRobot robot) {
        this.robot = robot;
    }

}
